/*build.cpp
 *This program builds RAID.
 *Created on: 2013.12.24
 *Author: Petrunin D.S.
 */

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring>

using namespace std;

int buildDisks(int nDisks, int nBlocks, int blockS, int RAIDN) {

	FILE* config = fopen("conf", "w");
  if (config == NULL) {
  	cout<<"Can't open config file.\n";
      return 1;
  }
  fprintf(config, "%i\n", nDisks);
	fprintf(config, "%i\n", nBlocks);
	fprintf(config, "%i\n", blockS);
	fprintf(config, "%i\n", RAIDN);
	fclose(config);
	char DiskNm[10];
	FILE* disk;
	char* diskData = new char[blockS*nBlocks];
	for(int i = 1; i <= nDisks; i++) {
	    sprintf(DiskNm, "Disk%i", i);
	    disk = fopen(DiskNm, "wb+");
	    if (disk == NULL) {
	        cout<<"Can't open file Disk" << i <<"\n";
	        return 1;
	    }
		fwrite(diskData, sizeof(char), blockS*nBlocks, disk);
		fclose(disk);
	}
	delete[] diskData;
	cout<<"Successfull build of RAID "<<RAIDN<<".\n";
	return 0;
}


int main(int argc, char* argv[]) {
	if (argc < 5) {
		cout << "Not enough parameters\n";
		return 1;
	}
	int nDisks = atoi(argv[1]);
		if ((nDisks < 2)||(nDisks > 5)) {
			cout << "Number of disks must be 2..5\n";
			return 1;
	}
	int nBlocks = atoi(argv[2]);
		if ((nBlocks < 100)||(nBlocks > 10000)) {
			cout << "Number of blocks must be 100..10000\n";
			return 1;
		}
	int blockS = atoi(argv[3]);
		if ((blockS < 256)||(blockS > 2048)) {
			cout << "Size of block must be 256..2048\n";
			return 1;
		}
	int RAIDN = atoi(argv[4]);
		if ((RAIDN < 0)||(RAIDN > 5)||(RAIDN == 2)) {
			cout << "Level of RAID must be 0, 1, 3, 4 or 5\n";
			return 1;
		}
	if ((RAIDN >= 3)&&(nDisks < 3)) {
		cout << "Not enough disks for chosen level of RAID\n";
		return 1;
	}
	return buildDisks(nDisks, nBlocks, blockS, RAIDN);
}







